package com.woniuxy.ServletContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @auther linke
 * @create 2021-10-19 14:35
 */
@WebServlet("/servletContextDemo01")
public class ServletContextDemo01 extends HttpServlet {
    /**
     * 根据参数获取MIME类型
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servletContextDemo03...执行了");
        //获取servletContext对象
        ServletContext servletContext = this.getServletContext();
        //获取MIME类型
        String mimeType1 = servletContext.getMimeType("demo.html");
        String mimeType2 = servletContext.getMimeType("aa.jpg");
        System.out.println(mimeType1+"..."+mimeType2);//text/html...image/jpeg

        //设置响应头
        resp.setHeader("content-type",mimeType1);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
